package com.zmn.plat.dubbo.impl.orderconfig.orderstock;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.dubbo.config.annotation.Service;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.business.interfaces.orderconfig.orderstock.OrderStockBService;
import com.zmn.plat.common.dto.orderconfig.orderstock.OrderStockDRO;
import com.zmn.plat.common.dto.orderconfig.orderstock.SpOrderStockDRO;
import com.zmn.plat.common.dto.orderconfig.orderstock.SpOrderStockWarningDRO;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.dubbo.interfaces.orderconfig.orderstock.OrderStockListRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;

import lombok.extern.slf4j.Slf4j;

/**
 * 描述：订单库存dubbo接口类
 *
 * @author: tanghongjun
 * @time: 2020/4/21 9:40
 */
@Slf4j
@Service(loadbalance = "roundrobin", timeout = 5000, version = DubboConsts.INTERFACE_VERSION)
public class OrderStockListRemoteServiceImpl implements OrderStockListRemoteService {

    @Resource
    OrderStockBService orderStockBService;

    @Override
    @ReadOnlyConnection
    public ResponseDTO<OrderStockDRO> getOrderStockStatusByCityAndDateAndShowProductId(Integer plat, Integer cityId, Date date, Integer showProductId) {
        log.error("getOrderStockStatusByCityAndDateAndShowProductId参数：plat:{}，cityId:{}，date:{}，showProductId:{}", plat, cityId, date, showProductId);
        try {
            OrderStockDRO dro = orderStockBService.getOrderStockStatusByCityAndDateAndShowProductId(plat, cityId, date, showProductId);
            return ResponseDTO.success(dro);
        } catch (PlatException e) {
            return ResponseDTO.fromErrorCodeTable(e.getErrorCode());
        } catch (Exception e) {
            log.error("getOrderStockStatusByCityAndDateAndShowProductId error.plat:{}，cityId:{}，date:{}，showProductId:{}", plat, cityId, date, showProductId, e);
            return ResponseDTO.fail("系统内部错误");
        }
    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<OrderStockDRO> getCityOrderStockByPlatAndCityAndCategAndDate(Integer plat, Integer cityId, Integer servCategId, Date date) {
        log.error("getOrderStockStatusByCityAndDateAndShowProductId参数：plat:{}，cityId:{}，date:{}，servCategId:{}", plat, cityId, date, servCategId);

        try {
            OrderStockDRO dro = orderStockBService.getCityOrderStockByPlatAndCityAndCategAndDate(plat, cityId, servCategId, date);
            return ResponseDTO.success(dro);
        } catch (PlatException e) {
            return ResponseDTO.fromErrorCodeTable(e.getErrorCode());
        } catch (Exception e) {
            log.error("getCityOrderStockByPlatAndCityAndCategAndDate error.plat:{}，cityId:{}，date:{}，servCategId:{}", plat, cityId, date, servCategId, e);
            return ResponseDTO.fail("系统内部错误");
        }

    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<OrderStockDRO> getSpOrderStockByPlatAndCityAndSpAndCategAndDate(Integer plat, Integer cityId, Integer spId, Integer servCategId, Date date) {
        log.info("getSpOrderStockByPlatAndCityAndSpAndCategAndDate 参数为：plat:{}，cityId:{}，date:{}，servCategId:{}，spId{}", plat, cityId, date, servCategId, spId);
        try {
            OrderStockDRO dro = orderStockBService.getSpOrderStockByPlatAndCityAndSpAndCategAndDate(plat, cityId, spId, servCategId, date);
            return ResponseDTO.success(dro);
        } catch (PlatException e) {
            return ResponseDTO.fromErrorCodeTable(e.getErrorCode());
        } catch (Exception e) {
            log.error("getSpOrderStockByPlatAndCityAndSpAndCategAndDate error.plat:{}，cityId:{}，date:{}，servCategId:{}，spId{}", plat, cityId, date, servCategId, spId, e);
            return ResponseDTO.fail("系统内部错误");
        }

    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<SpOrderStockDRO>> getSpOrderStockByPlatAndCityAndSpListAndCategAndDate(Integer plat, Integer cityId, List<Integer> spIdList, Integer servCategId, Date date) {
        log.info("getSpOrderStockByPlatAndCityAndSpAndCategAndDate 参数为：plat:{}，cityId:{}，date:{}，servCategId:{}，spIdList:{}", plat, cityId, date, servCategId, spIdList);

        try {
            List<SpOrderStockDRO> dros = orderStockBService.getSpOrderStockByPlatAndCityAndSpListAndCategAndDate(plat, cityId, spIdList, servCategId, date);
            return ResponseDTO.success(dros);
        } catch (PlatException e) {
            return ResponseDTO.fromErrorCodeTable(e.getErrorCode());
        } catch (Exception e) {
            log.error("getSpOrderStockByPlatAndCityAndSpListAndCategAndDate error.plat:{}，cityId:{}，date:{}，servCategId:{}，spIdList:{}", plat, cityId, date, servCategId, spIdList, e);
            return ResponseDTO.fail("系统内部错误");
        }
    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<SpOrderStockWarningDRO> getSpOrderStockWarning(Integer cityId, Integer spId, Integer servCategId, Integer categOneId, Integer categTwoId, Date date) {
        log.info("getSpOrderStockWarning 参数为：cityId:{}，date:{}，spId:{}，servCategId：{},categOneId：{}，categTwoId：{}", cityId, date, spId, servCategId, categOneId, categTwoId);
        try {
            SpOrderStockWarningDRO dro = orderStockBService.getSpOrderStockWarning(cityId, spId, servCategId, categOneId, categTwoId, date);
            return ResponseDTO.success(dro);
        } catch (PlatException e) {
            return ResponseDTO.fromErrorCodeTable(e.getErrorCode());
        } catch (Exception e) {
            log.error("getSpOrderStockWarning error.：cityId:{}，date:{}，spId:{}，servCategId：{},categOneId：{}，categTwoId：{}", cityId, date, spId, servCategId, categOneId, categTwoId, e);
            return ResponseDTO.fail("系统内部错误");
        }
    }
}
